package com.android.fragmentdemo.leanback
import android.os.Bundle
import android.view.KeyEvent
import androidx.leanback.widget.OnChildViewHolderSelectedListener
import androidx.recyclerview.widget.RecyclerView
import com.android.fragmentdemo.R
import com.android.fragmentdemo.adapters.FocusFloatRvAdapter
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityVgvcomposeBinding
import com.android.fragmentdemo.defineviews.Dy0202ItemDecoration
import com.hengfoxwolf.commonbase.base.util.LogX

class VGVComposeActivity : BaseActivity<ActivityVgvcomposeBinding>() {

    override fun getLayoutId() = R.layout.activity_vgvcompose

    override fun init(savedInstanceState: Bundle?) {

        LogX.init(VGVComposeActivity::class.java)

//        val mParentAOA = ArrayObjectAdapter(ListRowPresenter())
//
//        val mHGVPresenter = HGVPresenter(this@VGVComposeActivity)
//        val mChildAOA = ArrayObjectAdapter(mHGVPresenter)
//        mChildAOA.addAll(0, DataFactoryIp.createDataSet())
//        val mChildHeadA = HeaderItem(0, "HorizontalItem")
//        mParentAOA.add(ListRow(mChildHeadA, mChildAOA))
//
//        val mChildAOAB = ArrayObjectAdapter(VGVPresenter(this@VGVComposeActivity))
//        mChildAOAB.addAll(0, DataFactoryIp.createDataSet())
//        val mChildHeadB = HeaderItem(1, "VerticalItem")
//        mParentAOA.add(ListRow(mChildHeadB, mChildAOAB))
//
//        val mIBA = ItemBridgeAdapter(mParentAOA)
//        mBinding.vgvCompose.apply {
//            horizontalSpacing = 10
//            adapter = mIBA
//        }

        val dataSet = DataFactoryIp.createStrDataSet()
        val mAdapter = FocusFloatRvAdapter()
        mAdapter.itemDataSet = dataSet
        mBinding.vgvCompose.apply {
            adapter = mAdapter
            addItemDecoration(Dy0202ItemDecoration(resources.getDimension(R.dimen.dp_10)))
        }
        mBinding.vgvCompose.requestFocus()

        //从四个方向上查找下一个获取焦点的view
        //mBinding.vgvCompose.focusSearch(View.FOCUS_UP)
        //mBinding.vgvCompose.focusSearch(View.FOCUS_DOWN)
        //mBinding.vgvCompose.focusSearch(View.FOCUS_LEFT)
        //mBinding.vgvCompose.focusSearch(View.FOCUS_RIGHT)

        //mBinding.vgvCompose.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS) //优先获取焦点
        //mBinding.vgvCompose.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS)  //拦截截断焦点
        //mBinding.vgvCompose.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS)  //末尾获取焦点

        //设置按键监听事件
        //mBinding.vgvCompose.setOnKeyListener { targetView, keyCode, event ->
        //    return@setOnKeyListener true  //true表示消费拦截事件  事件不再传递
        //    return@setOnKeyListener false
        //}

        //焦点发生改变 获得与失去焦点
        //mBinding.llcRoot.setOnFocusChangeListener { targetView, hasFocus ->
        //    if (hasFocus) {
        //
        //    } else {
        //
        //    }
        //}

        //View.NO_ID表示阻止向下传递焦点
        //mBinding.llcRoot.nextFocusUpId = View.NO_ID
        //mBinding.llcRoot.nextFocusDownId = View.NO_ID
        //mBinding.llcRoot.nextFocusLeftId = View.NO_ID
        //mBinding.llcRoot.nextFocusRightId = View.NO_ID

        window.decorView.viewTreeObserver.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
            //全局监控获取焦点view与失去焦点view  oldFocus表示上一个获取焦点view  newFocus表示当前获取焦点view
        }

        //监听当前获取焦点的item
        mBinding.vgvCompose.setOnChildViewHolderSelectedListener(object : OnChildViewHolderSelectedListener() {
            override fun onChildViewHolderSelected(
                parent: RecyclerView?,
                child: RecyclerView.ViewHolder?,
                position: Int,
                subposition: Int
            ) {
                super.onChildViewHolderSelected(parent, child, position, subposition)
            }
        })

        //如果RecyclerView的丢失焦点的item的position与selectedPosition相同，
        //则表示RecyclerView当前没有item获取到焦点
    }

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        if (event?.action == KeyEvent.ACTION_DOWN) {
            when (event.keyCode) {
                KeyEvent.KEYCODE_DPAD_DOWN -> {
                    LogX.d("KeyEvent.KEYCODE_DPAD_DOWN")
                }
                KeyEvent.KEYCODE_DPAD_UP -> {
                    LogX.d("KeyEvent.KEYCODE_DPAD_UP")
                }
            }
        }
        return super.dispatchKeyEvent(event)
    }

    /*class SpaceItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            super.getItemOffsets(outRect, view, parent, state)
            outRect.right = 30
            outRect.bottom = 30
        }
    }*/

    companion object {
        const val TAG = "VGVComposeActivity"
    }
}